﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using CarFrameWork.Common;

namespace CarFrameWork.UI.ProductDesign._3DModelingDesign.ZhuJianSuQi
{
    public partial class 轴承盖 : PartFormBase
    {
        CarFrameWork.Model.CanShuHua.轴承盖 zcg = new CarFrameWork.Model.CanShuHua.轴承盖();

        public 轴承盖()
        {
            InitializeComponent();
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Global.SelectedID = string.Empty;///改动
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //if (!Panduan())
            //{
            //    return;
            //}

            if (zcg.Exists(Convert.ToInt32(this.cmbIDList.Text.Trim())) == true)
            {
                FormToModel();
                zcg.ID = Convert.ToInt32(this.cmbIDList.Text.Trim());
                zcg.Update();
                MessageBox.Show("更新成功", "提示");
                Global.SelectedID = this.cmbIDList.Text.Trim();
                this.Close();
            }
            else
            {
                if (TestContent())
                {
                    FormToModel();
                    zcg.Add();
                    MessageBox.Show("添加成功", "提示");
                    Global.SelectedID = this.cmbIDList.Text.Trim();
                    this.Close();
                }
                else
                    MessageBox.Show("参数不能为空！", "提示");
               
            }
        }

        private void FormToModel()
        {
            //zcg.ID = 2402102;
            zcg.name = "轴承盖";
            zcg.path = "path";
            zcg.D1 = Convert.ToSingle(textBox1.Text);
            zcg.D2 = Convert.ToSingle(textBox2.Text);
            zcg.D3 = Convert.ToSingle(textBox3.Text);
            zcg.D4 = Convert.ToSingle(textBox4.Text);
            zcg.D5 = Convert.ToSingle(textBox5.Text);
            zcg.L1 = Convert.ToSingle(textBox6.Text);
            zcg.L2 = Convert.ToSingle(textBox7.Text);
            zcg.L3 = Convert.ToSingle(textBox8.Text);
            zcg.L4 = Convert.ToSingle(textBox9.Text);
            zcg.L5 = Convert.ToSingle(textBox10.Text);
            zcg.L6 = Convert.ToSingle(textBox11.Text);
            zcg.L7 = Convert.ToSingle(textBox12.Text);
            zcg.L8 = Convert.ToSingle(textBox13.Text);
            zcg.L9 = Convert.ToSingle(textBox14.Text);
            zcg.L10 = Convert.ToSingle(textBox15.Text);
            zcg.L11 = Convert.ToSingle(textBox16.Text);
            zcg.L12 = Convert.ToSingle(textBox17.Text);
            zcg.L13 = Convert.ToSingle(textBox18.Text);
            zcg.L14 = Convert.ToSingle(textBox19.Text);
            zcg.L15 = Convert.ToSingle(textBox20.Text);
            zcg.R1 = Convert.ToSingle(textBox21.Text);
            zcg.R2 = Convert.ToSingle(textBox22.Text);
            zcg.R3 = Convert.ToSingle(textBox23.Text);
        }

     /*   private bool Panduan()
        {
            for (int i = 3; i < 26; i++)
            {
                TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                if (float.Parse(textbox.Text) <= 0)
                {
                    MessageBox.Show("对话框中的数值不能为零或负数!");
                    return false;
                }
            }

            if (float.Parse(textBox4.Text) < float.Parse(textBox3.Text))
            {
                MessageBox.Show("对话框中的D4<D2,数值不正确!");
                return false;
            }
            if (float.Parse(textBox2.Text) < float.Parse(textBox4.Text))
            {
                MessageBox.Show("对话框中的D1<D4,数值不正确!");
                return false;
            }
            if (float.Parse(textBox5.Text) < float.Parse(textBox6.Text))
            {
                MessageBox.Show("对话框中的H<H1,数值不正确!");
                return false;
            }

            return true;

        }*/

        private bool TestContent()
        {
            if (!InspectionText(this.textBox1))
                return false;
            if (!InspectionText(this.textBox2))
                return false;
            if (!InspectionText(this.textBox3))
                return false;
            if (!InspectionText(this.textBox4))
                return false;
            if (!InspectionText(this.textBox5))
                return false;
            if (!InspectionText(this.textBox6))
                return false;
            if (!InspectionText(this.textBox7))
                return false;
            if (!InspectionText(this.textBox8))
                return false;
            if (!InspectionText(this.textBox9))
                return false;
            if (!InspectionText(this.textBox10))
                return false;
            if (!InspectionText(this.textBox11))
                return false;
            if (!InspectionText(this.textBox12))
                return false;
            if (!InspectionText(this.textBox13))
                return false;
            if (!InspectionText(this.textBox14))
                return false;
            if (!InspectionText(this.textBox15))
                return false;
            if (!InspectionText(this.textBox16))
                return false;
            if (!InspectionText(this.textBox17))
                return false;
            if (!InspectionText(this.textBox18))
                return false;
            if (!InspectionText(this.textBox19))
                return false;
            if (!InspectionText(this.textBox20))
                return false;
            if (!InspectionText(this.textBox21))
                return false;
            if (!InspectionText(this.textBox22))
                return false;
            if (!InspectionText(this.textBox23))
                return false;
            return true;
        }
        private void 轴承盖_Load(object sender, EventArgs e)
        {
            foreach (DataRow row in GetIDList(string.Empty).Tables[0].Rows)
            {
                this.cmbIDList.Items.Add(row[0].ToString().Trim());
            }

            bool tag = false;
            foreach (string str in this.cmbIDList.Items)
            {
                if (str == this.m_ID.ToString())
                {
                    tag = true;
                    break;
                }
            }
            if (!tag)
                this.cmbIDList.Items.Add(this.m_ID.ToString().Trim());
            this.cmbIDList.SelectedIndex = 0;
            this.cmbIDList.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private DataSet GetIDList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID ");
            strSql.Append(" FROM [轴承盖] ");
            if (strWhere.Trim() != string.Empty)
            {
                if (strWhere != "ID = System.Data.DataRowView")
                    strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        private DataSet GetDataList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [轴承盖] ");
            if (strWhere.Trim() != string.Empty)
            {
                if (strWhere != "ID = System.Data.DataRowView")
                    strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        private void cmbIDList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModelToForm(GetDataList("ID = " + cmbIDList.Text.Trim()));
        }

        private void ModelToForm(DataSet dt)
        {
            if (!zcg.Exists(Convert.ToInt32(cmbIDList.Text.Trim())))
            {
                for (int i = 3; i < 26; i++)
                {
                    TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                    textbox.Text = "";
                }
            }
            else
            {
                for (int i = 3; i < 26; i++)
                {
                    TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                    textbox.Text = dt.Tables[0].Rows[0][i].ToString().Trim();
                }
            }
        }
    }
}